package com.test.deencredit.view_deencredit.process_widget_deencredit;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import static com.test.deencredit.config_deencredit.ProcessConfig_deencredit.Data_Action_Contact_Phone_deencredit;

import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.test.deencredit.R;
import com.test.deencredit.base_deencredit.BaseProcessWidgetManager_deencredit;
import com.test.deencredit.bean_deencredit.process_deencredit.ProcessInfo_deencredit;
import com.test.deencredit.bean_deencredit.process_deencredit.ProcessInfo_Data_deencredit;
import com.test.deencredit.config_deencredit.CountryConfig_deencredit;
import com.test.deencredit.util_deencredit.UserInfoUtil_deencredit;
import com.test.deencredit.util_deencredit.http_deencredit.HttpUtil_deencredit;

public class InputViewManager_deencredit extends BaseProcessWidgetManager_deencredit {

    private TextView titleTv_deencredit;
    private EditText inputEt_deencredit;

    public InputViewManager_deencredit(View view, ProcessInfo_Data_deencredit data, String page){
        super(view,data,page);
    }

    @Override
    protected void init_deencredit(){

        titleTv_deencredit = mView_deencredit.findViewById(R.id.vpi_tv_title);
        inputEt_deencredit = mView_deencredit.findViewById(R.id.vpi_et_input);

        setTitle_deencredit(mData_deencredit.getTitle_deencredit());
        inputEt_deencredit.setHint(mData_deencredit.getHint_deencredit());

        //联系人电话输入数字
        if (mData_deencredit.getAction_deencredit().equalsIgnoreCase(Data_Action_Contact_Phone_deencredit)) inputEt_deencredit.setInputType(InputType.TYPE_CLASS_NUMBER);

        //加纳卡号和卡名填充
        if (CountryConfig_deencredit.getInstance_deencredit().isGhana_deencredit() && mData_deencredit.getParamName_deencredit().equalsIgnoreCase("walletMobile")){
            inputEt_deencredit.setText(UserInfoUtil_deencredit.getInstance_deencredit().getMobile_deencredit());
        }
        if (CountryConfig_deencredit.getInstance_deencredit().isGhana_deencredit() && mData_deencredit.getParamName_deencredit().equalsIgnoreCase("walletUsername")){
            inputEt_deencredit.setText(ProcessInfo_deencredit.getInstance_deencredit().getRecipientName_deencredit());
        }

        inputEt_deencredit.setOnFocusChangeListener((v, hasFocus) -> HttpUtil_deencredit.getInstance_deencredit().saveBuriedPoint_deencredit(page_deencredit, mData_deencredit.getParamName_deencredit().toUpperCase()+"_INPUT",hasFocus ? "": getInput_deencredit()));

    }

    @Override
    public boolean checkInput_deencredit() {
        if (!mData_deencredit.isMustInput_deencredit()) return true;
        return !TextUtils.isEmpty(inputEt_deencredit.getText());
    }

    @Override
    public String getInput_deencredit() {
        return inputEt_deencredit.getText().toString().trim();
    }

    public void addTextChangeListener_deencredit(TextWatcher textWatcher){
        inputEt_deencredit.addTextChangedListener(textWatcher);
    }

    public void setTitle_deencredit(String title){
        if (TextUtils.isEmpty(title)) titleTv_deencredit.setVisibility(GONE);
        else {
            titleTv_deencredit.setVisibility(VISIBLE);
            titleTv_deencredit.setText(title);
        }
    }

}
